package com.xiaoyu.commonlib.utils;

import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;

import java.lang.reflect.Field;

/**
 * UI界面工具类
 * Created by jk on 2018/4/17.
 */

public class UIUtils {

    /**
     * 屏幕的宽度
     */
    private static int sScreenWidth;

    /**
     * 屏幕的高度
     */
    private static int sScreenHeight;

    /**
     * 屏幕的密度
     */
    private static float sDensity;

    /**
     * 状态栏的高度
     */
    private static int sStatusBarHeight;

    private UIUtils() {
        /**
         * 本界面工具类不能被实例化，直接调用静态方法就可以
         */
    }

    public static void init(Context context) {
        sDensity = context.getResources().getDisplayMetrics().density;
        Display display = getDisplay(context);

        Point realSize = new Point();
        int sdkVersion = Build.VERSION.SDK_INT;
        if (sdkVersion >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            display.getRealSize(realSize);
            sScreenWidth = realSize.x;
            sScreenHeight = realSize.y;
        } else {
            sScreenWidth = display.getWidth();
            sScreenHeight = display.getHeight();
        }
        initStatusBarHeight(context);
    }

    private static void initStatusBarHeight(Context context) {
        Class<?> c;
        Object obj;
        Field field;
        int x;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            sScreenHeight = context.getResources().getDimensionPixelOffset(x);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static Display getDisplay(Context context) {
        WindowManager windowManager = (WindowManager) context
                .getSystemService(context.WINDOW_SERVICE);
        return windowManager.getDefaultDisplay();
    }

    /**
     * 获取状态栏的高度
     */
    public static int getStatusBarHeight() {
        return sStatusBarHeight;
    }

    /**
     * 获取屏幕的宽度
     */
    public static int getScreenWidth() {
        return Math.min(sScreenWidth, sScreenHeight);
    }

    /**
     * 获取屏幕的宽度
     *
     * @param context
     * @return
     */
    public static int getScreenWidth(Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        int sreenWidth = dm.widthPixels;
        return sreenWidth;
    }


    /**
     * 获取屏幕的高度
     */
    public static int getScreenHeight() {
        return Math.max(sScreenWidth, sScreenHeight);
    }

    /**
     * 根据手机的分辨率将dp单位转换成px
     *
     * @param dpValue
     * @return
     */
    public static int dip2px(float dpValue) {
        return (int) (dpValue * sDensity + 0.5f);
    }

    /**
     * 根据手机的分辨率将dp单位转换成px
     *
     * @param context
     * @param dpValue
     * @return
     */
    public static int dip2px(Context context, float dpValue) {
        final float density = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * density + 0.5f);
    }


    //获取状态栏高度
    public static int getStatusBarHeight(Context context) {
        Class<?> c = null;
        Object obj = null;
        Field field = null;
        int x = 0, statusBarHeight = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            statusBarHeight = context.getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return statusBarHeight;
    }

    public static String initWebView(WebView mWebView, String contentUrl) {

        WebSettings webSettings = mWebView.getSettings();
        String strUrl = "<html> \n" +
                "<head> \n" +
                "<style type=\"text/css\"> \n" +
                "body {text-align:justify; font-size: " + 14 + "px; color:#303133; line-height: " + 21 + "px}\n" +
                " p {word-break:break-all} " +
                "</style> \n" +
                "</head> \n" +
                "<body>" +
                contentUrl +
                "</body> \n";
        webSettings.setJavaScriptEnabled(true);
        return strUrl;
    }

}










































































































































































































































































